// pages/detail/index.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
   courseCover:null,
   coursePrice:0,
   courseItem:[],
   courseContent:null,
   server:app.data.server,
   courseTitle:'',
   height:0,
   nextCourseHoursId:"",
    //处理onload onshow事件的执行次数
   options:{},

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this=this;
    wx.getSystemInfo({
      success: function(res) {
        _this.setData({
          height:res.windowHeight
        })
      },
    })

    options.userId = app.globalData.userInfo.id;

    _this.data.options = options;
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    var _this=this;
    var options1 = _this.data.options;
    debugger
    app.paiRequest("/app/course/tcourses/detail", options1).then(function (res) {

      let courseCover = res.data.courseCover;
      let courseItem = res.data.childList;
      let coursePrice = res.data.coursePrice;
      let courseContent = res.data.courseContent;
      let courseTitle = res.data.courseTitle;
      let teacherId = res.data.teacherId;



      _this.setData({
        courseCover: courseCover,
        courseItem: courseItem,
        coursePrice: coursePrice,
        courseContent: courseContent,
        courseTitle: courseTitle,
        teacherId: teacherId,
        lastCourseHours:true
      })
    })


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  selectHours:function(event) {
    var _this = this;
    var userInfo = app.globalData.userInfo;
    if (userInfo == null) {
      wx.navigateTo({
        url: '/pages/login/index',
      })
      return false;
    }

    var courseHourLock = event.currentTarget.dataset.lock;
    var index = event.currentTarget.dataset.index;
    var temp_index = index+1;
    var nextCourseHourseId;
    if(_this.data.courseItem[temp_index]){
      var courseHourseId = _this.data.courseItem[temp_index].id;
      _this.data.nextCourseHoursId = courseHourseId;

    }else{
      console.info("最后一题了")
      _this.data.nextCourseHoursId = undefined;
    }

    nextCourseHourseId = _this.data.nextCourseHoursId;

    if(courseHourLock==true){
      //todo 这个图标存在问题，可能要引用图标组件
      wx.showToast({
        title: '该课时未解锁，必须完成以解锁的课后练习。',
        icon: 'warn',
        duration: 2000,
        mask: false
      })
      return false
    }


    var id=event.currentTarget.dataset.id;
    var parentcode = event.currentTarget.dataset.parentcode;
    var coursehoname = event.currentTarget.dataset.coursehoname;


    var courseContent = this.data.courseContent;
    var courseTitle = this.data.courseTitle;
    var courseItem = this.data.courseItem;
    var teacherId = this.data.teacherId;
    let poster = this.data.courseCover;
    var sumitdata={
      courseId: parentcode,
      courseHourseId:id
    };
    //
    app.paiRequest("/app/course/study/tcourseStudy/startStudy", sumitdata).then(function (res) {
     var studyId=res.data.id;
     console.log(res);
      wx.navigateTo({
        url: '../coursehourse/index?id=' + id + '&courseTitle='
            + courseTitle + '&courseContent=' + courseContent + '&studyId=' + studyId
            + '&pauseTime=' + res.data.pauseTime + '&difference=' + res.data.difference +
            '&teacherId=' + teacherId + '&poster=' + poster + '&coursehoname=' + coursehoname
            +'&nextCourseHourseId='+nextCourseHourseId+"&lock="+courseHourLock,
      })
    })
    
  },
  paycourse:function(){
    wx.requestPayment({
      timeStamp: '',
      nonceStr: '',
      package: '',
      signType: 'MD5',
      paySign: '',
    })
  }
})